// pages/pt_list/pt_list.js
var app = getApp();
var utils = require('../../utils/shop/formatShop.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //产品信息
    productDetail: {},
    teamsList: [],     //产品对应的参团列表
    ptTeamArgs: {
      act: 'get_pintuan_team',
      Products_ID: 0,
      page:1,
      pageSize: 20
    },
    //加载更多动画
    loading: true,
    //请求状态
    get_flag: true,

    User_ID: 0,
  },

  //获得产品的详情
  productInfo: function (res) {
    if (res.errorCode === 0) {
      var productData = res.data[0];
      this.setData({
        productDetail: productData,
      });
    } else {
      wx.showModal({
        title: '错误',
        content: res.msg,
        showCancel: false,
        success: function () {
          wx.navigateBack({
            delta: 1
          });
        }
      })
    }

    // 结束下拉刷新
    utils.overPullDownRefresh();
  },

  //获取此产品的可参团团队
  getPtTeam () {
    this.setData({
      loading: true
    });
    app.http_req(this.data.ptTeamArgs, app.globalData.init.api_url, 'POST', (res) => {
      if (res.errorCode == 0) {
        var teamsList = res.data;
        for (var i in teamsList) {
          teamsList[i].user.User_HeadImg = utils.img_path(app.globalData.init.front_url, teamsList[i].user.User_HeadImg);

          // 是否已参过此团
          teamsList[i].is_join = app.globalData.userInfo && app.globalData.userInfo.User_ID > 0 && teamsList[i].join_user.includes(app.globalData.userInfo.User_ID) ? 1 : 0;
        }
        this.setData({
          teamsList: this.data.teamsList.concat(teamsList),
          'ptTeamArgs.page': this.data.ptTeamArgs.page + 1,
          get_flag: (res.totalCount / this.data.ptTeamArgs.pageSize) > this.data.ptTeamArgs.page ? true : false
        });
        //参团时间倒计时
        for (var i in this.data.teamsList) {
          utils.count_down(this, i);
        }
      } else if (res.errorCode == 2) {  //无产品
        this.setData({
          get_flag: false
        });
      } else {
        app.getError(res.errorCode, res.msg);
      }

      this.setData({
        loading: false
      });

      // 结束下拉刷新
      utils.overPullDownRefresh();
    });
  },

  //开团人头像加载失败处理（显示默认图片）
  show_default_userImg: function (e) {
    var index = e.currentTarget.dataset.index;
    var teamsList = this.data.teamsList;
    for (var i in teamsList) {
      if (index == i) {
        teamsList[i].user.User_HeadImg = '../../images/moren.png';
      }
    }
    this.setData({
      teamsList: teamsList
    });
  },

  load: function(){
    //检查用户是否登录
    if (!app.globalData.userInfo) {
      app.login('', this.load);
      return false;
    }

    //定义查询参团列表参数
    var ptTeamArgs = this.data.ptTeamArgs;
    this.setData( {
      'ptTeamArgs.Products_ID': this.data.ProductsID
    })
    var detailArgs = {
      act: "get_pro",
      Products_ID: this.data.ProductsID
    }
    //查询产品详情
    app.http_req(detailArgs, app.globalData.init.api_url, 'POST', this.productInfo);
    //如果用户登录，则查询购物车产品数量
    if (app.globalData.userInfo) {
      ptTeamArgs.User_ID = app.globalData.userInfo.User_ID;
      this.setData({
        User_ID: app.globalData.userInfo.User_ID
      });
    };

    //查询参团信息
    this.getPtTeam();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     if (options.proid) {
       var ProductsID = this.data.ProductsID;
        this.setData({
          ProductsID: options.proid
        })
     }
     this.load();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //显示加载导航条
    wx.showNavigationBarLoading();
    //初始化数据 (防止数据重复)
    this.setData({
      productDetail: {},
      teamsList: [],
      'ptTeamArgs.page': 1,
      //加载更多动画
      loading: true,
      //请求状态
      get_flag: true,

      User_ID: 0,
    });
    this.load();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.get_flag && !this.data.loading) {
      this.getPtTeam();
    }
  }
})